function load_image(data) {
    for (var i = 0; i < data.info.length; i++) {
        var row = data.info[i];
        var str = '        <div class="item" data-w="' + row.width + '" data-h="' + row.height + '">\n' +
            '          <a href="/picture/single/?pid=' + row.id + '">\n' +
            '            <img src="' + row.url + '" alt="">\n' +
            '          </a>\n' +
            '          <div>\n' +
            '            <div class="counts hide-xs hide-sm ">\n' +
            '              <em data-href="" class="ajax"><i class="iconfont  icon-speechbubble" style="pointer-events: none;"></i>\n' +
            '                ' + row.click_num + '</em>\n' +
            '              <em data-href="" class="ajax"><i class="iconfont icon-icon_like"\n' +
            '                  style="pointer-events: none;"></i> ' + row.star_num + '</em>\n' +
            '              <em data-location=""><i class="iconfont icon-favorite"></i> ' + row.parise_num + '</em>\n' +
            '            </div>\n' +
            '            <a href=""></a>\n' +
            '          </div>\n' +
            '        </div>';
        // 异步加载
        $('.flex-images').append(str);
    }
    changeImage();
    $('.flex-images').flexImages({rowHeight: 275});
    // 显示点赞收藏数
    $(".item").hover(
        function (e) {
            e.currentTarget.children[1].style.opacity = 1;
        },
        function (j) {
            j.currentTarget.children[1].style.opacity = 0;
        });
    // 更新当前页数
    var cur_page = $('#page_num').text();
    if (data.info.length < 25)
        $(".middle_more").hide();
    else
        $('#page_num').text(Number(cur_page) + 25);
}

//检测用户是否已登录
function is_log() {
    $.get('/user/session_get/', function (data, status) {
    var jsonObj = JSON.parse(data);
    var code = jsonObj['status'];
    if (code == '200'){
        $('.img1').attr("src", jsonObj['info']['avatar']);
        $('.welcome').text('欢迎您：'+ jsonObj['info']['name']);
        $('.welcome').css('display','');
        $('.login-out').css('display','');
        $('.login1').css('display', 'none');
        $('.register1').css('display','none');
    }
});
}

//用户登录成功信息刷新
function login_right(user_id) {
    $.get('/user/user_avatar/',{'user_id':user_id},function (data,status) {
                    var jsonObj1 = JSON.parse(data);
                    var avatar = jsonObj1['avatar'];
                    $('.img1').attr("src", avatar);
                    $('.welcome').text('欢迎您：'+ jsonObj1['name']);
                    $('.welcome').css('display','');
                    $('.login-out').css('display','');
                    $('.login1').css('display', 'none');
                    $('.register1').css('display','none');
                });
}


var search_content = $('#search-input').val();
var targets = [];

$(function () {
    function search(types) {
        // 加载页清零
        $('.page_num').text(0);
        // 显示加载更多按钮
        if ($(".middle_more").css('display') == 'none')
            $(".middle_more").show();
        $.get('/search_result/', {
            'types': types,
            'content': search_content,
            'targets': JSON.stringify(targets),
        }, function (data) {
            data = JSON.parse(data);
            $('.flex-images').html("");
            $('.result').html('已找到' + data.nums + '条结果');
            console.log(data)
            if (data.status == 200) {
                $(".no_find").css("display", "none");
                load_image(data);
            } else {
                // 404，500状态码处理
                $(".no_find").css("display", "block");
                $(".middle_more").hide();
            }
        })
    }

    search(1);

    // 异步加载标签
    $.get('/picture/targets/', {}, function (data) {
       data = JSON.parse(data);
       console.log(data);
       if (data.status == 200) {
           console.log(data);
           // 加载为你推荐标签
           for (var i=0; i < data.info.recommend.length; i++) {
               var row = data.info.recommend[i];
               var str = '<span class="more-l">' + row.target + '</span>';
               $('.recommend').append(str);
           }

           // 加载场景标签
           for (var i=0; i < data.info.sence.length; i++) {
               var row = data.info.sence[i];
               var str = '<span class="more-l">' + row.target + '</span>';
               $('.sence').append(str);
           }

           // 当点击标签，进行过滤
           $('.more-line span').click(function () {
               if ($(this).hasClass('active')) {
                   $(this).removeClass('active');
                   targets.splice($.inArray($(this).text(), targets), 1);
               } else {
                   $(this).addClass('active');
                   targets.push($(this).text());
               }
               search(1);
               // console.log(targets);
           })
       } else {}
    });

    // 点击全部
    $('.all').click(function () {
        targets = [];
        search(1);
        $('.more-line span').removeClass('active');
    });

    // 当点击搜索按钮，异步刷新
    $('.search').click(function () {
        search_content = $('#search-input').val();
        var types = 0;
        if ($('.picker').text().indexOf("图片") != -1)
            types = 1;

        // 如果在电影搜索页面搜索图片，进行全局搜索
        if (types == 0)
            window.location.href = '/search/?types=' + types + '&content=' + search_content;
        else
            search(types);
    });

    // 加载更多
    $('.pure-button').click(function () {
        $.get('/search_result/', {
            'types': 1,
            'content': search_content,
            'targets': JSON.stringify(targets),
            'nums': $('#page_num').text(),
        }, function (data) {
            data = JSON.parse(data);
            console.log(data);
            // 处理异常
            if (data.status == 200) {
                load_image(data);

            } else {
            }
        })
    })
});